﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Web.myadmin
{
    public partial class domainTime : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //SessionHelp.AssertLogin(this); //登录验证

            #region 显示正确的布局
            string cmd = Request.QueryString["cmd"];
            showTheRightRegion(cmd);
            switch (cmd)
            {
                case "add": initAddForm();break;
                case "edit":initEditFrom();break;
                case "view": initViewTable(); break;
                case "delete": delete(); break;
                default: showInfoList(); break;
            }
            #endregion
        }

        #region 信息列表和查询
        /// <summary>
        /// 查询数据
        /// </summary>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            showInfoList();
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        protected void showInfoList()
        {
            #region 变量定义
            DataSet dset = new DataSet();
            BZZ.DAL.domainTime dal = new BZZ.DAL.domainTime();
            System.Collections.Generic.List<System.Data.SqlClient.SqlParameter> paraList = new List<System.Data.SqlClient.SqlParameter>();
            string dataFields = ""; //读取的字段名，空为所有
            string strWhere = "1=1"; //查询子句
            string strOrderBy = " enddate asc"; //排序参数，如果需要分页，则必填 
            #endregion

            #region 查询条件
            //string strId = txtQryId.Text.Trim();
            //if (strId != "" && strId != null )
            //{
            //    strWhere += " and id=@id";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@id", strId));
            //} 
            string strSiteName = txtQrySiteName.Text.Trim();
            if (strSiteName != null && strSiteName != "" )
            {
                strWhere += " and siteName=@siteName";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@siteName", strSiteName));
            } 
            string strDomain = txtQryDomain.Text.Trim();
            if (strDomain != null && strDomain != "" )
            {
                strWhere += " and domain=@domain";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@domain", strDomain));
            } 
            //string strRegDate = txtQryRegDate.Text.Trim();
            //if (strRegDate != "" && strRegDate != null )
            //{
            //    strWhere += " and regDate=@regDate";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@regDate", strRegDate));
            //} 
            //string strEndDate = txtQryEndDate.Text.Trim();
            //if (strEndDate != "" && strEndDate != null )
            //{
            //    strWhere += " and endDate=@endDate";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@endDate", strEndDate));
            //} 
            //string strIcpno = txtQryIcpno.Text.Trim();
            //if (strIcpno != "" && strIcpno != null )
            //{
            //    strWhere += " and icpno=@icpno";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@icpno", strIcpno));
            //} 
            //string strFeeUnit = txtQryFeeUnit.Text.Trim();
            //if (strFeeUnit != "" && strFeeUnit != null )
            //{
            //    strWhere += " and feeUnit=@feeUnit";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@feeUnit", strFeeUnit));
            //} 
            //string strMemo = txtQryMemo.Text.Trim();
            //if (strMemo != "" && strMemo != null )
            //{
            //    strWhere += " and memo=@memo";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@memo", strMemo));
            //} 
            //string strExt1 = txtQryExt1.Text.Trim();
            //if (strExt1 != "" && strExt1 != null )
            //{
            //    strWhere += " and ext1=@ext1";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext1", strExt1));
            //} 
            //string strExt2 = txtQryExt2.Text.Trim();
            //if (strExt2 != "" && strExt2 != null )
            //{
            //    strWhere += " and ext2=@ext2";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext2", strExt2));
            //} 
            #endregion

             #region 分页控件绑定
             int pageSize = 30; //分页每页记录个数
             int totalRecourd = 0; //总记录个数
             int currentPage = 1; //当前页码
             try { currentPage = int.Parse(Request.QueryString["p"]); }
             catch { }

             totalRecourd = dal.GetCount(strWhere, paraList);
             dset = dal.GetDatasetByPageSql(currentPage, pageSize, strWhere, strOrderBy, paraList, dataFields);
             if (dset!=null && dset.Tables[0].Rows.Count > 0)
             {
                 rptInfoList.DataSource = dset;
                 rptInfoList.DataBind();
                 litPager.Text = LIB.pager.getPageHtml(this, totalRecourd, pageSize);
             }
             else
             {
                 if (paraList.Count > 0)
                     litPromptMessage.Text = "没有查询到相关记录 ";
                 else
                 {
                     litPromptMessage.Text = " <a href='?cmd=add'>添加新纪录</a>";
                     divQuery.Visible = false;
                 }
             } 
             #endregion
             #region 单页数据绑定
             //strWhere += " " + strOrderBy;
             //dset = dal.GetDataset(strWhere, paraList, dataFields);
             //if (dset.Tables[0].Rows.Count > 0)
             //{
             //    rptInfoList.DataSource = dset;
             //    rptInfoList.DataBind();
             //}
             //else
             //{
             //    litPromptMessage.Text = "没有查询到相关记录";
             //} 
             #endregion

        }
        #endregion

        public string getEndDate(object oDate)
        {
            DateTime endDate = Convert.ToDateTime(oDate);
            int n = DateDiff(DateTime.Today, endDate, "day");
            string s = n.ToString();
            if (n < 60) s = "<font color=red size=+1><b>" + n.ToString() + "</b></font>";
            return s;
        }

        private int DateDiff(DateTime dtStart, DateTime dtEnd, string unit)
        {
            TimeSpan tsCount = dtEnd - dtStart;
            switch (unit)
            {
                case "day":
                    return (int)tsCount.TotalDays;
                case "hour":
                    return (int)tsCount.TotalHours;
                case "minute":
                    return (int)tsCount.TotalMinutes;
                case "second":
                    return (int)tsCount.TotalSeconds;
                case "millisecond":
                    return (int)tsCount.TotalMilliseconds;
            }
            return 0;
        }

        #region 添加、修改、删除操作
        /// <summary>
        /// 添加修改记录
        /// </summary>
        protected void btnSumit_Click(object sender, EventArgs e)
        {
            #region 获取数据
            bool isAdd = false;
            int varId ;	//id
            string varSiteName;	//网站名称
            string varDomain;	//域名
            DateTime varRegDate ;	//注册时间
            DateTime varEndDate ;	//到期时间
            string varIcpno;	//备案号
            string varFeeUnit;	//续费单位
            string varMemo;	//备注
            string varExt1;	//扩展1
            string varExt2;	//扩展2
            try
            {
               varId =int.Parse(txtId.Text.Trim());	//id
               varSiteName =txtSiteName.Text.Trim();	//网站名称
               varDomain =txtDomain.Text.Trim();	//域名
               varRegDate =DateTime.Parse(txtRegDate.Text.Trim());	//注册时间
               varEndDate =DateTime.Parse(txtEndDate.Text.Trim());	//到期时间
               varIcpno =txtIcpno.Text.Trim();	//备案号
               varFeeUnit =txtFeeUnit.Text.Trim();	//续费单位
               varMemo =txtMemo.Text.Trim();	//备注
               varExt1 = "";// txtExt1.Text.Trim();	//扩展1
               varExt2 = "";// txtExt2.Text.Trim();	//扩展2
            }
            catch
            {
               LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
               return;
            }
            if (varId == 0) isAdd = true;
            #endregion

            #region 赋值model
            BZZ.DAL.domainTime dal = new BZZ.DAL.domainTime();
            BZZ.Model.domainTime model = new BZZ.Model.domainTime();
            if (isAdd)
            {
                model.siteName = varSiteName;	//网站名称
                model.domain = varDomain;	//域名
                model.regDate = varRegDate;	//注册时间
                model.endDate = varEndDate;	//到期时间
                model.icpno = varIcpno;	//备案号
                model.feeUnit = varFeeUnit;	//续费单位
                model.memo = varMemo;	//备注
                model.ext1 = varExt1;	//扩展1
                model.ext2 = varExt2;	//扩展2
                if (dal.Add(model) > 0)
                {
                    LIB.JsWrite.AlertAndGo(this, "添加成功", "?cmd=list");
                }
                else
                {
                    LIB.JsWrite.AlertAndGoback(this,"操作失败");
                }
            }
            else
            {
                model = dal.GetModel(varId);
                model.id = varId;	//id
                model.siteName = varSiteName;	//网站名称
                model.domain = varDomain;	//域名
                model.regDate = varRegDate;	//注册时间
                model.endDate = varEndDate;	//到期时间
                model.icpno = varIcpno;	//备案号
                model.feeUnit = varFeeUnit;	//续费单位
                model.memo = varMemo;	//备注
                model.ext1 = varExt1;	//扩展1
                model.ext2 = varExt2;	//扩展2
				if (dal.Update(model) > 0)
                {
                    LIB.JsWrite.Alert(this, "修改成功");
                }
                else
                {
                    LIB.JsWrite.AlertAndGoback(this,"操作失败");
                }
            }
  
            #endregion
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        protected void delete()
        {
            #region 删除操作
            int id = 0;
            try { id = int.Parse(Request.QueryString["id"]); }
            catch
            {
                //litPromptMessage.Text = "非法的参数";
                LIB.JsWrite.Alert(this, "非法的参数");
                return;
            }

            BZZ.DAL.domainTime dal = new BZZ.DAL.domainTime();
            if (dal.Delete(id)>0)
            {
                //litPromptMessage.Text = "删除成功";
                LIB.JsWrite.AlertAndGo(this, "删除成功", "?cmd=list");
            }
            else
            {
                //litPromptMessage.Text = "操作失败";
                LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
            } 
            #endregion
        }
        #endregion

        #region 初始化添加表单
        /// <summary>
        /// 初始化添加表单
        /// </summary>
        protected void initAddForm()
        {
            lblFormTitle.Text = "添加信息";
            if (!IsPostBack)
            {

                txtSiteName.Text = "";
                txtDomain.Text = "";
                txtRegDate.Text = "";
                txtEndDate.Text = "";
                txtIcpno.Text = "";
                txtFeeUnit.Text = "";
                txtMemo.Text = "";
                //txtExt1.Text = "";
                //txtExt2.Text = "";
            }
        }
        #endregion

        #region 初始化修改表单
        /// <summary>
        /// 初始化修改表单
        /// </summary>
        protected void initEditFrom()
        {
            int id =Convert.ToInt32(Request.QueryString["id"]);
            lblFormTitle.Text = "修改信息";
            if (!IsPostBack)
            {
                BZZ.Model.domainTime model = new BZZ.Model.domainTime();
                BZZ.DAL.domainTime dal = new BZZ.DAL.domainTime();
                model = dal.GetModel(id);

                txtId.Text = model.id.ToString();
                txtSiteName.Text = model.siteName.ToString();
                txtDomain.Text = model.domain.ToString();
                txtRegDate.Text = model.regDate.ToString();
                txtEndDate.Text = model.endDate.ToString();
                txtIcpno.Text = model.icpno.ToString();
                txtFeeUnit.Text = model.feeUnit.ToString();
                txtMemo.Text = model.memo.ToString();
                //txtExt1.Text = model.ext1.ToString();
                //txtExt2.Text = model.ext2.ToString();
            }
        }
        #endregion

        #region 初始化查看表格
        /// <summary>
        /// 初始化查看表格
        /// </summary>
        protected void initViewTable()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            lblViewTitle.Text = "查看信息";
            if (!IsPostBack)
            {
                BZZ.Model.domainTime model = new BZZ.Model.domainTime();
                BZZ.DAL.domainTime dal = new BZZ.DAL.domainTime();
                model = dal.GetModel(id);

                lblSiteName.Text = model.siteName.ToString();
                lblDomain.Text = model.domain.ToString();
                lblRegDate.Text = model.regDate.ToString();
                lblEndDate.Text = model.endDate.ToString();
                lblIcpno.Text = model.icpno.ToString();
                lblFeeUnit.Text = model.feeUnit.ToString();
                lblMemo.Text = model.memo.ToString();
                lblExt1.Text = model.ext1.ToString();
                lblExt2.Text = model.ext2.ToString();
            }
        }

        #endregion

        #region 自定义函数

        /// <summary>
        /// 函数功能：示例函数
        /// 作者：buzhizhe 创建时间：2011.6.22
        /// 修改者：buzhizhe 最后修改时间：2011.6.22
        /// </summary>
        protected void YourFunction()
        {
            ///
        }


        #endregion


        #region 公共函数，不需要修改

        /// <summary>
        /// 根据cmd参数判断应该显示哪些部分内容
        /// </summary>
        /// <param name="cmd"></param>
        protected void showTheRightRegion(string cmd)
        {
            #region show or hide div
            switch (cmd)
            {
                case "add":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "edit":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "view":
                    divView.Visible = true;
                    divForm.Visible = false;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "delete":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
                case "query":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
                default:
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
            }
            #endregion
        }
        #endregion



    }


}

